package lnu.project.cart;

import lnu.project.cart.entity.Basket;

import lnu.project.cart.provider.BasketProvider;
import lnu.project.cart.provider.IBasketProvider;
import lnu.project.cart.provider.IProductProvider;
import lnu.project.cart.provider.ProductProvider;
import android.app.Activity;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EnterPhonenum extends Activity {

	private IProductProvider productProvider;
	private IBasketProvider basketProvider;

	private Basket mybasket;
	private Activity this_activity3;

	Button btnSendSMS;
	EditText txtPhoneNo;
	EditText txtMessage;
	Activity this_activity;

	/** Called when the activity is first created */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.enternumber_xml);
		this_activity3 = this;
		btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
		txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);

		btnSendSMS.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String phoneNo = txtPhoneNo.getText().toString();
				// set text message
				InitProviders();
				Bundle bundle = getIntent().getExtras();
				if (bundle != null) {
					String sid = bundle.getString("basketId");
					mybasket = basketProvider.Get(Long.parseLong(sid));

				
					mybasket = productProvider.LoadProduct(mybasket);
				
					float cost = 0;
					String message = "SHOPPING BAG: " + mybasket.getName()
							+ " WITH PRODUCTS: ";
					for (int count = 0; count < mybasket.getProducts().size(); count++, message += ", ") {
						if (mybasket.getProducts().get(count)
								.getTypeOfProduct() == 0)
							message += mybasket.getProducts().get(count)
									.getQuantity()
									+ " "
									+ mybasket.getProducts().get(count)
											.getName();

						else if (mybasket.getProducts().get(count)
								.getTypeOfProduct() == 1) {
							message += mybasket.getProducts().get(count)
									.getWeight()
									+ "Kg "
									+ mybasket.getProducts().get(count)
											.getName();
							
							
						}
						cost += mybasket.getProducts().get(count)
								.GetTotalPrice();

					}
					message+="WITH TOTAL COST: "+cost+" euros.";
					if (phoneNo.length() > 0 && message.length() > 0) {

						sendSMS(phoneNo, message);
						finish();

					}

					else
						Toast.makeText(getBaseContext(),
								"Please enter both phone number and message.",
								Toast.LENGTH_SHORT).show();

				}

			}

			private void sendSMS(String phoneNumber, String message) {
				PendingIntent pi = PendingIntent.getActivity(this_activity3, 0,
						null, 0);
				SmsManager sms = SmsManager.getDefault();
				sms.sendTextMessage(phoneNumber, null, message, pi, null);
			}
		});
	}

	private void InitProviders() {
		this.productProvider = new ProductProvider();
		this.productProvider.SetActivity(this_activity3);

		this.basketProvider = new BasketProvider();
		this.basketProvider.SetActivity(this_activity3);
	}

}